/*
 * @version: 1.0.0
 * @Author: fl
 * @Date: 2020-07-06 11:50:33
 * @LastEditTime: 2020-09-14 15:00:54
 */
import Taro from '@tarojs/taro';
import { BASE_URL, wid } from '@/config';
import { login } from './auth';
import Notify from '@components/vant/notify/notify';
function checkSuccess(data) {
  Taro.stopPullDownRefresh();
  Taro.hideNavigationBarLoading();

  if (data instanceof ArrayBuffer && typeof data === 'string') {
    return data;
  }

  if (data.statusCode >= 200 && data.statusCode < 300) {
    return data.data;
  }
  if (data.statusCode === 401) {
    return login();
  }

  const { data: respData } = data;
 
  const message = respData.msg || '服务器异常';
  // Taro.atMessage({ message, type: 'error' });
  const error = new Error(message);
  error.data = respData.data;
  error.text = respData.msg;
  error.code = data.statusCode;
  throw error;
}

function throwError(err) {
 
  Taro.hideNavigationBarLoading();
  const error = new Error(err.errMsg || '服务器正在维护中!');
  error.code = 500;
  throw error;
}

const http = {
  request(url, options) {
    Taro.showNavigationBarLoading();
    return Taro.request({
      ...options,
      url: `${BASE_URL}${url}`,
      header: {
        ...options.header,
      },
    })
      .then(res => {
        return checkSuccess(res);
      })
      .then(res => {
        if (!res.code) {
          Taro.hideLoading();
          return res;
        } else {
          //短提示
          Taro.hideLoading();
          // if (res.code == 1) {
          //   Taro.showToast({
          //     icon: 'none',
          //     title: res.msg || '访问出错',
          //   });
          // } else {
          //   Notify({
          //     message: res.msg || '访问出错',
          //     duration: 3000,
          //   });
          // }
          // return res;
          const message = res.msg || '服务器异常';

          const error = new Error(message);
          error.code = res.code;
          error.data = res.data;
          error.msg = res.msg;
          throw error;
        }
      })
      .catch(error => {
  
        if (error.code==2) {
          Notify({
                 message: error.message || '访问出错',
                 duration: 3000,
               });
               throw error;
        }
        Taro.showToast({
          icon: 'none',
          title: error.message || error.errMsg || '访问出错！',
        });
        throw error;
       
      });
  },
  get(url, data = {}, options = {}) {
    return this.request(url, {
      data,
      ...options,
    });
  },
  post(url, data = {}, options = {}) {
    var that = this;
    let sessionId = Taro.getStorageSync('session_id');
    if (url.indexOf('?') > 0) {
      url += '&wid=' + wid + '&session_id=' + sessionId;
    } else {
      url += '?wid=' + wid + '&session_id=' + sessionId;
    }
    return this.request(url, {
      method: 'POST',
      data: data,
      ...options,
    });
  },
  put(url, data = {}, options = {}) {
    return this.request(url, {
      method: 'PUT',
      data: data,
      ...options,
    });
  },
  delete(url, data = {}, options = {}) {
    return this.request(url, {
      method: 'DELETE',
      data: data,
      ...options,
    });
  },
};

export default http;
